import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { HttpService } from '../../service/httpService';
import { DetailsPage } from './details';


@Component({
  selector: 'page-activity',
  templateUrl: 'activity.html',
})
export class ActivityPage{

  private navs:any;
  private showLoading:boolean = true;
  private activitys:any;
  constructor(private http:HttpService,
              private nav:NavController) {
    this.initData();
  }

  private initData(){

    let url = '/api/Promo/GetAllType';
    let self = this;
    this.http.get(url,null,function (res,err) {
      if (!err){
        self.navs = res;
        for(var i=0;i<self.navs.length;i++){
          self.navs[i].select = false;
          self.navs[i].activitys = [];
        }
        self.navs[0].select = true;
        if (self.navs.length > 0){
          self.getTypeAction(0);
        }
      }
    });
  }

  detailsAction(index){
    let obj = this.activitys[index];
    this.nav.push(DetailsPage,{"model":obj});
  }

  private getTypeAction(index){
    let obj = this.navs[index];
    let url = '/api/Promo/GetList';
    let map = {
      type : obj.Id,
      isMobile:true,
      pageSize:20
    };
    let self = this;
    self.showLoading = true;
    this.http.get(url,map,function (res,err) {
      self.showLoading = false;
      if (!err){
        let list = res.list;
        for(var i=0;i<list.length;i++){
          list[i].Img = self.http.imageUrl + list[i].Img;
        }
        self.activitys = list;
        self.navs[index].activitys = list;
      }
    });
  }

  changeNav(index){
    for(var i=0;i<this.navs.length;i++){
      this.navs[i].select = false;
    }
    this.navs[index].select = !this.navs[index].select;
    let obj = this.navs[index].activitys;
    this.activitys = obj;
    if (obj.length <= 0){
      this.getTypeAction(index);
    }
  }



}
